import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.sql.*;

public class editServicesFrame extends JDialog
{
 
  /* Frame */
  editServicesFrame frame;
  Canvas canvas;
  JPanel panel;
  JLabel title;
  
  JTextField serviceName;
  JTextField price;
  JTextArea description;
  JScrollPane scrollPane;
  JCheckBox available;
  
  JButton btnSaveChanges;
  JButton btnCancel;
  
  JLabel backgroundImage;
  
  public editServicesFrame(JFrame frame, String str){
            super(frame, str);
            addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    MainFrame.frame.setEnabled(true);
                }
            });
    }
    
  public void MainFrame(){
      
        /* Frame Options */
        frame = new editServicesFrame(new JFrame(),"Edit Service Details");
        frame.setSize(370,320);

        /* Panel Options*/
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(370, 370));
        panel.setLayout(null);
        
            title = new JLabel("Edit Service Details ");
            title.setFont(new Font("Microsoft Sans Serif", Font.PLAIN, 16));
            title.setForeground(Color.white);
            title.setBounds(20,0,270,30);
            panel.add(title);
        
            title = new JLabel("Service Name: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,20,170,50);
            panel.add(title);
            
            title = new JLabel("Price: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,55,170,50);
            panel.add(title);
       
            title = new JLabel("Description: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,90,170,50);
            panel.add(title);
            
            title = new JLabel("Availability: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,195,250,50);
            panel.add(title);
            
            try{
              // Get a statement from the connection
              MainFrame.database.stmt = MainFrame.database.conn.createStatement();
              // Execute the query
                               
              MainFrame.database.rs = MainFrame.database.stmt.executeQuery("SELECT * FROM SERVICE WHERE SERVICEID = "+ servicesPage.picked );
                               
               while( MainFrame.database.rs.next() ){
                      /* Service Name input */
                      serviceName = new JTextField(MainFrame.database.rs.getString("serviceName"));
                      serviceName.setBounds(150,35,190,25);
                      serviceName.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                      panel.add(serviceName);
                    
                      /* Service Name input */
                      price = new JTextField("" + MainFrame.database.rs.getFloat("price"));
                      price.setBounds(150,70,190,25);
                      price.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                      panel.add(price);
                      
                       /* Description of Service Input */
                       description = new JTextArea(MainFrame.database.rs.getString("serviceDescription"));
                       description.setEditable(true);
                       description.setLineWrap(true);
                       description.setFont(new Font("Cambria", Font.PLAIN, 14));
                       scrollPane = new JScrollPane(description);
                       scrollPane.setBounds(150,105,190,100);
                       scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                       panel.add(scrollPane);
                       
                         /* Availability Checkbox */
                         available = new JCheckBox ("Available");
                         available.setBounds(150,200,200,40);
                         available.setOpaque(false);
                         available.setFont(new Font("Cambria", Font.PLAIN, 14));
                        if(MainFrame.database.rs.getString("availability").equals("Available"))
                            available.setSelected(true);
                        else
                            available.setSelected(false);
                         panel.add(available);
                        
                }
            } catch( SQLException se ){}
            
            /* Save Changes Button */
            btnSaveChanges = new JButton(new ImageIcon("saveChangesButton.jpg"));
            btnSaveChanges.setBounds(50,250,114,25);
            btnSaveChanges.addActionListener(new ButtonListener());
            panel.add(btnSaveChanges);
            
            /* Cancel Button */
            btnCancel = new JButton(new ImageIcon("cancelButton.jpg"));
            btnCancel.setBounds(174,250,114,25);
            btnCancel.addActionListener(new ButtonListener());
            panel.add(btnCancel);

            /* Background Image */
            backgroundImage = new JLabel(new ImageIcon("bgedit.jpg"));  
            backgroundImage.setBounds(0,0,370,370);
            panel.add(backgroundImage);
        
            /* Frame Operations */
            frame.getContentPane().add(panel);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
  }
  
    public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){
       if(e.getSource() == btnSaveChanges){
           // Save changes made.
            try{
                        MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                        String avail;
                        if(available.isSelected() == true)
                            avail = "Available";
                            else
                            avail = "Unavailable";
                        
                                 int numUpdated = 
                                 MainFrame.database.stmt.executeUpdate( "UPDATE SERVICE SET serviceName = '" + serviceName.getText() + "', price = '" 
                                 + Float.valueOf(price.getText().trim()).floatValue() + "', serviceDescription = '" + description.getText() 
                                 + "', availability = '" + avail +
                                 "' WHERE ServiceId = " + servicesPage.picked);
                                 
                        JOptionPane.showMessageDialog(null,"All changes made are successfully saved.");
                         frame.dispose();
           
                         MainFrame.panel.removeAll();
                         MainFrame.servicesPage.Draw();
                         MainFrame.header.Draw();
                         MainFrame.panel.add(MainFrame.backgroundImage);
                         MainFrame.panel.repaint();
            
                         MainFrame.frame.setEnabled(true);
                         MainFrame.frame.toFront();
                         MainFrame.frame.setState(Frame.NORMAL);
            } catch( SQLException se ){}
           
        }
       else if(e.getSource() == btnCancel){
           frame.dispose();
           MainFrame.frame.setEnabled(true);
           MainFrame.frame.toFront();
           MainFrame.frame.setState(Frame.NORMAL);
        }
    }
   }
  }  

